﻿// Copyright 2008 Chris Varley (http://www.contractdriven.net/)
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//    http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

using System.Collections.Generic;
using System.Globalization;

namespace ContractDriven.CommonAssertions
{
    #region CollectionContainsAssertion
    /// <summary>
    /// Verifies that a collection does not contain a given object
    /// </summary>
    /// <typeparam name="T">The type of the object to be verified</typeparam>
    public class CollectionContainsAssertion<T> : BaseCollectionAssertion<T>
    {
        #region Constructors

        /// <summary>
        /// Verifies that a collection contains a given object, using a comparer.
        /// </summary>
        /// <typeparam name="T">The type of the object to be compared</typeparam>
        /// <param name="expected">The object that is expected not to be in the collection</param>
        /// <param name="collection">The collection to be inspected</param> 
        public CollectionContainsAssertion(T expected, IEnumerable<T> collection, IComparer<T> comparer)
            : base(expected, collection, comparer)
        {
            FailureDescription = string.Format(CultureInfo.CurrentCulture, Resource.CollectionContainsAssertion, expected.ToString());
        }

        /// <summary>
        /// Verifies that a collection contains a given object, using a comparer.
        /// </summary>
        /// <typeparam name="T">The type of the object to be compared</typeparam>
        /// <param name="expected">The object that is expected not to be in the collection</param>
        /// <param name="collection">The collection to be inspected</param> 
        public CollectionContainsAssertion(T expected, IEnumerable<T> collection)
            : this(expected, collection, AssertComparer<T>.GetComparer())
        {
            
        }

        #endregion

        #region Eval Method
        public override bool Eval()
        {
            foreach (T item in Collection)
                if (Comparer.Compare(Expected, item) == 0)
                    return true;

            return false;
        }
        #endregion
    }
    #endregion
}
